package koratSerializedTests;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import tp.TreeList;


public class Deserialize {

	public ObjectInputStream ois;

	public Deserialize(String fileName) throws FileNotFoundException,
			IOException {
		FileInputStream file = new FileInputStream(new File(fileName));
		ois = new ObjectInputStream(file);
	}

	public ArrayList<TreeList> deserialize() {
		ArrayList<TreeList> list = new ArrayList<TreeList>();
		try {
			TreeList tree = getNextTree();
			while (tree != null) {
				list.add(tree);
				tree = getNextTree();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	private TreeList getNextTree() throws ClassNotFoundException, IOException {
		try {
			return (TreeList) ois.readObject();
		} catch (EOFException eof) {
			return null;
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		}
	}
}
